﻿Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sFuncName As String = String.Empty
        Dim sErrDesc As String = String.Empty
        Dim oARInvoice As AE_EASIPOS_INTEGRATION_DLL.clsARInvoice = New AE_EASIPOS_INTEGRATION_DLL.clsARInvoice
        Dim oDataSetInvoice As DataSet = Nothing

        Try

            sFuncName = "Create_ARInvoice()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)

            'Getting the Parameter Values from App Cofig File
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling GetSystemIntializeInfo()", sFuncName)
            If GetSystemIntializeInfo(p_oCompDef, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)


            P_sConString = String.Empty
            P_sConString = "Data Source=" & p_oCompDef.p_sServerName & ";Initial Catalog=" & p_oCompDef.p_sIntDBName & ";User ID=" & p_oCompDef.p_sDBUserName & "; Password=" & p_oCompDef.p_sDBPassword
            AE_EASIPOS_INTEGRATION_DLL.P_sConString = P_sConString

            'Getting the Data from Invoice Table as DataSet
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ARInvoice_DataSet()", sFuncName)
            oDataSetInvoice = oARInvoice.ARInvoice_DataSet(P_sConString)

            If Not oDataSetInvoice Is Nothing Then

                'Function to connect the Company
                If p_oCompany Is Nothing Then
                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ConnectToTargetCompany()", sFuncName)
                    If ConnectToTargetCompany(p_oCompany, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)
                End If

                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling ARInvoice()", sFuncName)
                If oARInvoice.ARInvoice(oDataSetInvoice, p_oCompany, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

            End If

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)

        Catch ex As Exception
            Call WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
        End Try
    End Sub


    Function Document_Creation(ByVal oBaseType As SAPbobsCOM.BoObjectTypes, ByVal oTargetType As SAPbobsCOM.BoObjectTypes _
                               , ByRef oBaseComp As SAPbobsCOM.Company, ByRef oTarComp As SAPbobsCOM.Company _
                               , ByVal sDocEntry As String, ByRef IsDraft As Boolean) As Long
        Try
            Dim lRetCode As Long
            Dim oBaseDoc As SAPbobsCOM.Documents = oBaseComp.GetBusinessObject(oBaseType)
            Dim oTarDoc As SAPbobsCOM.Documents
            If oBasedoc.GetByKey(sDocEntry) Then

                If IsDraft = False Then
                    oTarDoc = oTarComp.GetBusinessObject(oTargetType)
                Else
                    oTarDoc = oTarComp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                    oTarDoc.DocObjectCode = oTargetType
                End If

                oTarDoc.CardCode = oBaseDoc.CardCode
                oTarDoc.DocDueDate = oBaseDoc.DocDueDate
                oTarDoc.TaxDate = oBaseDoc.TaxDate
                oTarDoc.DocDueDate = oBaseDoc.DocDueDate

                Dim count As Integer = oBaseDoc.Lines.Count - 1

                Dim oTargetLines As SAPbobsCOM.Document_Lines = oTarDoc.Lines

                For i As Integer = 0 To count
                    If i <> 0 Then
                        oTargetLines.Add()
                    End If
                    oTargetLines.BaseType = oBaseType
                    oTargetLines.BaseEntry = sDocEntry
                    oTargetLines.BaseLine = i
                Next
                lRetCode = oTarDoc.Add()

            Else
                Return RTN_SUCCESS
            End If

            Return RTN_SUCCESS
        Catch ex As Exception
            Return RTN_ERROR
        End Try
    End Function

    

End Class
